import React, { useCallback, useContext, useEffect } from 'react'
import { Space, Button, Radio } from 'antd'
import { useDesigner, TextWidget } from '@designable/react'
import { GlobalRegistry } from '@designable/core'
import { observer } from '@formily/react'
import { loadInitialSchema, saveSchema } from '../service'
import { QiankunContext } from '@/context/QiankunContext'

export const ActionsWidget = observer(() => {

  const {qiankun} = useContext(QiankunContext)
  useEffect(()=>{
    if(!Object.keys(qiankun).length) return
    qiankun.onGlobalStateChange((state:any)=>{
      console.log("子应用获取到数据",state)
      loadInitialSchema(designer,state.value)
    })
    qiankun.setGlobalState({type:'init',value:null})
  },[qiankun])
  
  const designer = useDesigner()
  // useEffect(() => {
  //   loadInitialSchema(designer)
  // }, [])
  const supportLocales = ['zh-cn', 'en-us', 'ko-kr']
  useEffect(() => {
    if (!supportLocales.includes(GlobalRegistry.getDesignerLanguage())) {
      GlobalRegistry.setDesignerLanguage('zh-cn')
    }
  }, [])


  const onSave = useCallback(()=>{
    qiankun.setGlobalState({type:'connect',value:saveSchema(designer)})
  },[qiankun])
  
  return (
    <Space style={{ marginRight: 10 }}>
      {/* <Button href="">
        表单设计器
      </Button> */}
      {/* <Radio.Group
        className='hidden'
        value={GlobalRegistry.getDesignerLanguage()}
        optionType="button"
        options={[
          { label: 'English', value: 'en-us' },
          { label: '简体中文', value: 'zh-cn' },
          { label: '한국어', value: 'ko-kr' },
        ]}
        onChange={(e) => {
          GlobalRegistry.setDesignerLanguage(e.target.value)
        }}
      /> */}
      {/* <Button className='hidden' href="https://github.com/alibaba/designable" target="_blank">
        <GithubOutlined />
        Github
      </Button> */}
      <Button
        onClick={onSave}
      >
        保存
      </Button>
      <Button
        type="primary"
        onClick={() => {
          saveSchema(designer)
        }}
      >
        发布
      </Button>
    </Space>
  )
})
